<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Watch 选项 监控数据</title>
    <script type="text/javascript" src="../assets/js/vue.js"></script>
</head>
<body>
<h1>Watch 选项 监控数据</h1>
<hr>
<div id="app">
    <p>今天温度：{{temp}}</p>
    <p>穿衣建议：{{message}}</p>
    <p>
        <button @click="add">升温</button>
        <button @click="sub">降温</button>
    </p>
</div>
<script type="text/javascript">
  var msgArray = [
    'T恤短裙', '夹克长裙', '棉衣羽绒服'
  ]
  var app = new Vue({
    el: '#app',
    data: {
      temp: 14,
      message: '夹克长裙'
    },
    methods: {
      add () {
        this.temp += 5
      },
      sub () {
        this.temp -= 5
      }
    },
    //方法1，在实例内部定义watch选项
    // watch:{
    //   temp(newVal,oldVal){
    //       if(newVal>25) {
    //         this.message=msgArray[0]
    //       }else if(newVal >0) {
    //         this.message = msgArray[1]
    //       }else {
    //         this.message = msgArray[2]
    //       }
    //   }
    // }
  })
  //方法2，在实例外部定义watch选项
  app.$watch('temp', function (newVal, oldVal) {
    if (newVal > 25) {
      this.message = msgArray[0]
    } else if (newVal > 0) {
      this.message = msgArray[1]
    } else {
      this.message = msgArray[2]
    }
  })
</script>
</body>
</html>